//
//  MessageVC.swift
//  mmda
//
//  Created by 李陆涛 on 2018/5/30.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit

class MessageVC: BaseTabVC {

    var viewModel = MessageViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "我的公告"
        self.tableView.rowHeight = 44;
        self.tableView.tableFooterView = UIView()
        
        viewModel.loadData()
        
        viewModel.loadFinished = {
            
            [weak self] error in
            guard let `self` = self else { return }
            
            self.showToast(error)
            self.tableView.reloadData()
            
        }
    }
}

extension MessageVC {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return viewModel.dataSource?.count ?? 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        
        if cell == nil {
            
            cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
            
            cell?.textLabel?.font = kFont14
            cell?.textLabel?.textColor = UIColor.darkGray
        }
        
        if let message = viewModel.dataSource?[indexPath.row] {
            
            cell?.textLabel?.text = message.title
        }
       
        return cell!
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        if let message = viewModel.dataSource?[indexPath.row] {
            
            let webVC = WebVC()
            webVC.url = message.url
            webVC.title = message.title
            
            self.show(webVC, sender: nil)
        }
    }
}
